package com.home.duy.code.DAO;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.home.Entity.TTGoiMon;
import com.home.System.HibernateUtil;
import com.home.duy.code.Abstract.AbstractDAO;

public class TTGoiMonDAO extends AbstractDAO{
	private Session session;
	private Transaction trans;
	public TTGoiMonDAO()
	{
		super();
	}
	
	@Override
	public boolean saveOrUpdate(Object obj) {
		return super.saveOrUpdate(obj);
	}

	@Override
	public boolean delete(Object obj) {
		return super.delete(obj);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object findByPk(Class clas, long pk) {
		return super.findByPk(clas, pk);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List findAll(Class clas) {
		return super.findAll(clas);
	}
	
	// =================== TRUC SECTION =================
	
	@SuppressWarnings("unchecked")
	public int DemSoMonGoi(long ban)
	{
		startOperation();
		int count = 0;
		try {
			List<TTGoiMon> L = findAll(TTGoiMon.class);
			for(TTGoiMon temp : L)
			{
				if(temp.getBan().getMaSo() == ban)
					count++;
			}
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			session.close();
		}
		return count;
	}
	
	//phuong thuc tim danh sach goi mon cua ban
	@SuppressWarnings("unchecked")
	public List<TTGoiMon> TimDanhSachGoiMon(long ban) {
		startOperation();
		List<TTGoiMon> list = null;
		try {
			list = session
			.createSQLQuery(
					"SELECT * FROM TTGOIMON WHERE TTM_BAN = "
							+ ban)
			.addEntity(TTGoiMon.class).list();
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			session.close();
		}
		
		return list;
	}
	// ==================== END TRUC SECTION ====================
	private void startOperation()
	{
		session = HibernateUtil.getSessionFactory().openSession();
		trans = session.beginTransaction();
	}
}
